package com.why.websocketmqttstudy.why.runner;


import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistry;
import com.why.websocketmqttstudy.why.mqtt.client.MqttConsumerClient;
import com.why.websocketmqttstudy.why.mqtt.config.MqttConfig;
import com.why.websocketmqttstudy.why.mqtt.handle.HandlerFactory;
import com.why.websocketmqttstudy.why.websocket.service.NettyServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;

@Configuration
public class InitApplicationRunner implements CommandLineRunner, ApplicationContextAware{

	private ApplicationContext applicationContext;
	
	@Autowired
	private com.why.websocketmqttstudy.why.websocket.service.NettyServer NettyServer;
	
	@Autowired
	private NacosServiceRegistry nacosServiceRegistry;
	
	@Autowired
	private HandlerFactory handlerFactory;
	

    public static MqttConfig mqttCofig = new MqttConfig();
		
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	@Override
	public void run(String... args) throws Exception {
		
		// 连接MQTT
		MqttConsumerClient client = new MqttConsumerClient(handlerFactory,mqttCofig);
		client.connect();
		
		//Properties properties = new Properties();
       // properties.setProperty("serverAddr", "127.0.0.1:8848");
       // properties.setProperty("namespace", "642f35f1-a89f-4b29-8f90-fd1e87b4bc7c");
        
    	//NamingService namingService = NamingFactory.createNamingService(properties);
        //将服务注册到注册中心
        //namingService.registerInstance("springboot-netty", InetAddress.getLocalHost().getHostAddress(), Integer.valueOf("12345"));

//		String serviceName = applicationContext.getEnvironment().getProperty("spring.application.name");
		String serviceName = "websocket-mqtt-study";
//		String serverAddr = applicationContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.server-addr");
		String serverAddr = "nacos://127.0.0.1:8848";
//		String namespace = applicationContext.getEnvironment().getProperty("spring.cloud.nacos.discovery.namespace");
		String namespace = "b69aeb1f-aff8-4ff5-8c24-561c41213b46";
//		String port = applicationContext.getEnvironment().getProperty("server.port");
		String port = "9001";

    	// nacos注册中心
          NacosDiscoveryProperties nacosDiscoveryProperties = new NacosDiscoveryProperties();
          // 服务名
          nacosDiscoveryProperties.setService(serviceName);
          // 端口
          nacosDiscoveryProperties.setPort(Integer.valueOf(port));
          // ip地址,容器化环境中获取ip地址可以使用spring-cloud-commons包中的inetUtils.findFirstNonLoopbackHostInfo().getIpAddress()
          nacosDiscoveryProperties.setIp(InetAddress.getLocalHost().getHostAddress());
          // 注册中心地址
          nacosDiscoveryProperties.setServerAddr(serverAddr);
          nacosDiscoveryProperties.setNamespace(namespace);
          
          NacosRegistration registration = new NacosRegistration(null, nacosDiscoveryProperties, applicationContext);
          nacosServiceRegistry.register(registration);
          
    	  try 
    	  {   
    		  NettyServer.start(Integer.valueOf(port)); 
    	  }
    	  catch(Exception e) 
    	  { 
    		  System.out.println("NettyServerError:"+e.getMessage()); 
    	  }
	}

}
